// 内容code
var code = tool.getUrlParam("code");

/**
 * 0初始化加载（loading）
 * 2播放进行（控制层关闭，按返回状态改为3）
 * 3播放暂停（控制层开启）
 * 4播放结束（控制层长显示）
 */
var playState = 0;
var videoInfo = {}; //播放信息
var moveOnceProgress = 0.02; //一次快进快退时间占比
var moveOnceTime = 0; //一次快进快退时间，每次初始化过程计算
var timerControl = null;

var timerPlay = null;

var todo = function () {
  if (!code) {
    // 退出
    videoError("无效内容编码");
    return;
  }
  createBox();
  bindEvent();
  loadData();
};
todo();

function createBox() {
  box0 = new eZone.BoxInstance(1, 1, [-1, -1, -1, -1], 0);
  box0.notShake = true;
  Wrapper.init();
}

function bindEvent() {
  box0.boxOkEvent = function () {
    if (playState == 2) {
      // 暂停并显示
      player.pause();
      // stopTimerPlay();
      $("btnPlay").class("btnPlay");
      $("playControl").css("opacity", "1");
      playState = 3;
    } else if (playState == 3) {
      player.play();
      $("btnPlay").class("btnPlay pause");
      $("playControl").css("opacity", "0");
      playState = 2;
    }
  };

  // 回退
  box0.leftEvent = function () {
    if (playState == 2 || playState == 3) {
      stopTimerPlay();
      $("playControl").css("opacity", "1");

      var next = videoInfo.currentTime - moveOnceTime;
      if (next < 0) {
        next = 0;
      }

      setSeek(next);
      setControlHideTimer();
    }
  };

  // 快进
  box0.rightEvent = function () {
    if (playState == 2 || playState == 3) {
      stopTimerPlay();
      $("playControl").css("opacity", "1");
      var next = videoInfo.currentTime + moveOnceTime;
      if (next > videoInfo.totalTime) {
        next = videoInfo.totalTime;
      }

      setSeek(next);
      setControlHideTimer();
    }
  };
}

var timerSeek = null;
function setSeek(next) {
  // 先更新到进度条
  videoInfo.currentTime = next;
  $("curTime").html(getTimeFormat(videoInfo.currentTime));
  setProgress();
  // 延迟执行继续播放
  clearTimeout(timerSeek);
  timerSeek = setTimeout(function () {
    player.setSeek(next);
    startTimerPlay();
  }, 500);
}

function loadData() {
  reportPage("play");
  getVideoData(function () {
    // 开始播放
    player.create({
      url: videoInfo.videoUrl,
    });
  });
}

function videoError(numb) {
  msg.toast("播放错误，即将退出，错误代码：" + numb, true, true);
  setTimeout(function () {
    goBack();
  }, 2000);
}

function getVideoData(success) {
  http.get({
    url: "/industry-center-local/api/epg/hotelVideo/{code}",
    params: {
      code: code,
    },
    success: function (res) {
      if (res.code == 200 && res.data) {
        videoInfo = res.data;
        success && success(res.data);
      } else {
        videoError("未找到内容");
      }
    },
    error: function () {
      videoError("查询内容请求失败");
    },
  });
}

function updateProgress() {
  videoInfo.currentTime = player.getNowTime();
  $("curTime").html(getTimeFormat(videoInfo.currentTime));
  setProgress();
  if (videoInfo.currentTime == videoInfo.totalTime && (playState == 2 || playState == 3)) {
    playState = 4;
    goBack();
  }
}

function startTimerPlay() {
  clearInterval(timerPlay);
  updateProgress();
  timerPlay = setInterval(updateProgress, 1000);
}

function stopTimerPlay() {
  clearInterval(timerPlay);
}
// 初始化参数
function playerInit() {
  playState = 2;
  player.play();
  moveOnceTime = Math.ceil(videoInfo.totalTime * moveOnceProgress);
  $("totalTime").html(getTimeFormat(videoInfo.totalTime));
  $("videoTitle").html(videoInfo.name);
  $("playControl").css("opacity", "1");
  // $("loading").hide();
  setControlHideTimer();
}

// 设置进度条
function setProgress() {
  var numb = (videoInfo.currentTime / videoInfo.totalTime) * 100;
  $("progress_inner").width(numb + "%");
}

// 设置自动关闭控制层(在重新开始播放、调节进度后调用，不改变状态)
function setControlHideTimer() {
  clearTimeout(timerControl);
  timerControl = setTimeout(function () {
    $("playControl").css("opacity", "0");
  }, 2000);
}

// 工具
function getNumbFormat(numb) {
  if (numb < 10) {
    return "0" + numb;
  }
  return "" + numb;
}
function getTimeFormat(time) {
  time = Math.ceil(time / 1000);
  var s = getNumbFormat(time % 60);
  time = Math.floor(time / 60);
  var m = getNumbFormat(time % 60);
  time = Math.floor(time / 60);
  var h = getNumbFormat(time);
  return h + ":" + m + ":" + s;
}

var isPageDie = false;
// 退出按键响应方法
function goBack() {
  if (isPageDie) {
    return;
  }
  isPageDie = true;
  clearTimeout(timerPlay);
  player.destory();
}

function eventHandler(code, desc) {
  //1000:用户按下返回键
  //1036:时移完成
  //5202:开始连接
  //5203:连接失败
  //5205:开始播放
  //5206:播放失败
  //5207:数据接收超时
  //5208:网络播放异常
  //5210:播放完成
  //5221:开始缓冲
  //5222:播放缓冲
  //5223:缓冲结束
  //5229:准备下载
  //5230:开始下载
  //5231:下载完成
  switch (code) {
    case 1000:
    case 5210:
      goBack();
      break;
    case 5205:
      playerInit();
      break;
    default:
      break;
  }
}
